package com.library.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import com.jtjrenren.android.taxi.driver.base.BaseApplication;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日志记录
 */
public class LogUtil {
    private static String SAVE_PATH_FOLDER = FileUtils.getSDCardPath() + File.separator + "rrcx" + File.separator + "log";
    private static String SAVE_PATH_FILENAME = "log";
    private static String DEFULT_TAG = "LogUtil";

    /* 开发阶段*/
    public static final int DEVELOP = 0;
    /*内部测试阶段*/
    public static final int DEBUG = 1;
    /* 公开测试*/
    public static final int BATE = 2;
    /* 正式版*/
    public static final int RELEASE = 3;

    /* 当前阶段标示*/
    public static int currentStage = DEBUG;


    public static void log(String msg) {
        log(DEFULT_TAG, msg);
    }

    public static void log(Class clazz, String msg) {
        log(clazz.getSimpleName(), msg);
    }

    public static void log(String tag, String msg) {
//        if (tag.equals("debug")) {
//            String log = null;
//            try {
//                log = getLogMsg(msg);
//                PushWorks.upLog(log);
//            } catch (PackageManager.NameNotFoundException e) {
//                e.printStackTrace();
//            }
//
//        }

        if (currentStage == DEVELOP) {
            Log.i(tag, msg);
        } else if (currentStage == DEBUG) {
            Log.i(tag, msg);
        } else if (currentStage == BATE) {

        } else if (currentStage == RELEASE) {

        }
    }

    public static void log_d(String msg) {
        log_d(DEFULT_TAG, msg);
    }

    public static void log_d(Class clazz, String msg) {
        log_d(clazz.getSimpleName(), msg);
    }

    public static void log_d(String tag, String msg) {
        if (currentStage == DEVELOP) {
            Log.d(tag, msg);
        } else if (currentStage == DEBUG) {
        } else if (currentStage == BATE) {

        } else if (currentStage == RELEASE) {

        }
    }


    public static void log_w(String msg) {
        log_w(DEFULT_TAG, msg);
    }

    public static void log_w(Class clazz, String msg) {
        log_w(clazz.getSimpleName(), msg);
    }

    public static void log_w(String tag, String msg) {
        if (currentStage == DEVELOP) {
            Log.w(tag, msg);
        } else if (currentStage == DEBUG) {
            Log.w(tag, msg);
        } else if (currentStage == BATE) {

        } else if (currentStage == RELEASE) {

        }
    }

    public static void log_v(String msg) {
        log_v(DEFULT_TAG, msg);
    }

    public static void log_v(Class clazz, String msg) {
        log_v(clazz.getSimpleName(), msg);
    }

    public static void log_v(String tag, String msg) {
        if (currentStage == DEVELOP) {
            Log.v(tag, msg);
        } else if (currentStage == DEBUG) {
        } else if (currentStage == BATE) {

        } else if (currentStage == RELEASE) {

        }
    }


    public static void log_e(String msg) {
        log_e(DEFULT_TAG, msg);
    }

    public static void log_e(Class clazz, String msg) {
        log_e(clazz.getSimpleName(), msg);
    }

    public static void log_e(String tag, String msg) {
        if (currentStage == DEVELOP) {
            Log.e(tag, msg);
        } else if (currentStage == DEBUG) {
            saveLogToSd(msg);
        } else if (currentStage == BATE) {

        } else if (currentStage == RELEASE) {

        }
    }

    public static void saveLogToSd(String log) {
        log = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\r\n" + log + "\r\n";
        FileUtils.saveFileCache(log.getBytes(), SAVE_PATH_FOLDER, SAVE_PATH_FILENAME);
    }

    public static String getLogMsg(String log) throws PackageManager.NameNotFoundException {
        String lineSeparator = "\r\n";
        StringBuilder sb = new StringBuilder();

        sb.append(SystemTool.getDataTime("yyyy-MM-dd-HH-mm-ss")).append(lineSeparator);

        // 应用的版本名称和版本号
        PackageManager pm = BaseApplication.getInstance().getApplicationContext().getPackageManager();
        PackageInfo pi = pm.getPackageInfo(BaseApplication.getInstance().getApplicationContext().getPackageName(),
                PackageManager.GET_ACTIVITIES);
        sb.append("App Version: ");
        sb.append(pi.versionName);
        sb.append('_');
        sb.append(pi.versionCode);
        sb.append(lineSeparator).append(lineSeparator);

        // android版本号
        sb.append("OS Version: ");
        sb.append(Build.VERSION.RELEASE);
        sb.append("_");
        sb.append(Build.VERSION.SDK_INT);
        sb.append(lineSeparator).append(lineSeparator);

        // 手机制造商
        sb.append("Vendor: ");
        sb.append(Build.MANUFACTURER);
        sb.append(lineSeparator).append(lineSeparator);

        // 手机型号
        sb.append("Model: ");
        sb.append(Build.MODEL);
        sb.append(lineSeparator).append(lineSeparator);

        // cpu架构
        sb.append("CPU ABI: ");
        sb.append(Build.CPU_ABI);
        sb.append(lineSeparator).append(lineSeparator);


        //exception的详细信息
        sb.append("LogUP:");
        sb.append(log);


        return sb.toString();
    }
}
